﻿using Microsoft.Extensions.Logging;

namespace _03_Logging.Extensions.File
{
    internal class FileLogger : ILogger
    {
        private readonly string categoryName;
       
        public IExternalScopeProvider ScopeProvider { get; set; }
        
        public FileLogger(string categoryName, IExternalScopeProvider scopeProvider)
        {
            this.categoryName = categoryName;
            this.ScopeProvider= scopeProvider;
        }
       
        public IDisposable BeginScope<TState>(TState state)
        {
            return ScopeProvider.Push(state);
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return logLevel != LogLevel.None;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
        {
            var currentPath = Directory.GetCurrentDirectory();
            var path = Path.Combine(currentPath, $"{DateTime.Now:yyyy-MM-dd}.log");
            var message = formatter(state,exception);
            System.IO.File.AppendAllText(path, $"{logLevel}: {categoryName} {DateTime.Now}\r\n{message}\r\n");
        }
    }
}
